package net.sf.nlp.ltag.parser.earley;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import net.sf.nlp.ltag.data.LTAGTree;

import net.sf.nlp.ltag.parser.LTAGState;
import net.sf.nlp.ltag.parser.ParserTree;

public class EarleyAlg extends EarleyAlgAbstract
{
	protected static Logger logger =  Logger.getLogger(EarleyAlg.class);
	
	public EarleyAlg(Map<String, List<LTAGTree>> ltagMap)
	{
		super(ltagMap);
	}

	public List<ParserTree> calculate() throws Exception
	{
		List<EarleyStateSet> stateSets = new ArrayList<EarleyStateSet>();
		
		EarleyStateSet initStateSet = getInitEarleyStateSet();
		
		stateSets.add(initStateSet);
		
		for (int step = 0; step < stateSets.size() && step <= words.size(); step++)
		{
			EarleyStateSet stateSet = stateSets.get(step);
			
			for (int index = 0; index < stateSet.getStates().size(); index++)
			{
				EarleyState state = stateSet.getStates().get(index);
				
				calculateTask(stateSets, state, step);
			}
			
			//-------------------------------------------
			for (int i = 0; i < stateSet.getStates().size(); i++)
			{	
				if (logger.isInfoEnabled())
				{
					EarleyState s = stateSet.getStates().get(i);
					logger.info(s.toString());
				}
			}
		}
		
		List<LTAGState> matchedStates = checkMatchedState(stateSets);
		List<ParserTree> parserTrees = getParsedTree(matchedStates);
		
		return parserTrees;
	}
}
